﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
namespace Group16.Class_Library
{
    public class DeviceManager
    {
        private List<Device> _devices;
        public List<Device> Devices
        {
            get 
            {
                if (_devices == null)
                    _devices = new List<Device>();
                    return _devices; 
            }
            set { _devices = value; }
        }
        public List<Device> SearchDevices(string name)
        {
            try
            {
                string comText = "Select * from Devices Where Name = '"+name+"' And Sold = 'false'" ;
                using (SqlDataReader reader = (SqlDataReader)DataBaseInteraction.ExecuteCommand(comText , 2))
                {
                    _devices = new List<Device>();
                    while (reader.Read())
                    {
                        Device dv = new Device();
                        dv.Field = reader.GetString(0);
                        dv.Title = reader.GetString(1);
                        dv.Series = reader.GetString(2);
                        dv.Name = name;
                        dv.Origin = reader.GetString(4);
                        dv.Warranty = (Int32)reader.GetValue(5);
                        dv.Discription = reader.GetString(6);
                        dv.Cost = (Int32)reader.GetValue(7);
                        dv.Sold = (bool)reader.GetValue(8);
                        Devices.Add(dv);
                    }
                }

            }
            catch (SqlException se)
            {
                MessageBox.Show("Problem in interaction with database" + se.Message);
            }
            finally
            {
                DataBaseInteraction.Connection.Close();
            }
            return Devices;
        }
        public void AddDevice(string series)
        {
            try
            {
                string comText = "Select * from Devices Where Series = '" + series + "'" ;
                using (SqlDataReader reader = (SqlDataReader)DataBaseInteraction.ExecuteCommand(comText, 2))
                {
                    if (reader == null) return;
                    reader.Read();
                    Device dv = new Device();
                    dv.Field = reader.GetString(0);
                    dv.Title = reader.GetString(1);
                    dv.Series = series;
                    dv.Name = reader.GetString(3);
                    dv.Origin = reader.GetString(4);
                    dv.Warranty = (Int32)reader.GetValue(5);
                    dv.Discription = reader.GetString(6);
                    dv.Cost = (Int32)reader.GetValue(7);
                    dv.Sold = (bool)reader.GetValue(8);
                    Devices.Add(dv);
                }
            }
            catch (SqlException se)
            {
                MessageBox.Show("Unable to add device to list " + se.Message);
            }
            finally
            {
                DataBaseInteraction.Connection.Close();
            }
        }
    }
}
